﻿
/*******************************************************************************************
// ©2009 Microsoft Corporation.  This code is provided under the Microsoft Public License.
*******************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Rtc.Collaboration.Presence;
using Microsoft.Rtc.Collaboration;

namespace ClientBilling
{
    /// <summary>
    /// Handles the publishing of presence for the ApplicationEndpoint
    /// </summary>
    public class PresencePublisher
    {
        // This is the XML that makes the endpoint appear available.
        const string AGGREGATE_STATE_XML = "<state xmlns=\"http://schemas.microsoft.com/2006/09/sip/state\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"aggregateState\"><availability>3500</availability></state>";

        // This is the XML that identifies the endpoint as an "automaton," so that it will always appear to be online.
        const string AUTOMATON_XML = "<contactCard xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://schemas.microsoft.com/2006/09/sip/contactcard\"><automaton>true</automaton></contactCard>";

        private ApplicationEndpoint _endpoint;

        public PresencePublisher(ApplicationEndpoint endpoint)
        {
            _endpoint = endpoint;
        }

        /// <summary>
        /// Publish presence information that causes the endpoint to appear available always.
        /// </summary>
        public void PublishAlwaysAvailable()
        {
            // Construct presence category objects with the XML presence information.
            CustomPresenceCategory appStateCategory = new CustomPresenceCategory("state", AGGREGATE_STATE_XML);
            CustomPresenceCategory contactCardCategory = new CustomPresenceCategory("contactCard", AUTOMATON_XML);

            // Create a PresenceCategoryWithMetaData object for the state presence category.
            PresenceCategoryWithMetaData stateMetaData = new PresenceCategoryWithMetaData(
               1 /* instanceId  */,
               0 /* default container */,
               appStateCategory /* state*/);

            // This state category will be published only once.
            stateMetaData.ExpiryPolicy = ExpiryPolicy.Persistent;

            // Create a PresenceCategoryWithMetaData object for the contact card presence category.
            PresenceCategoryWithMetaData contactCardMetaData = new PresenceCategoryWithMetaData(
                           5 /* instanceId  */,
                           0 /* default container */,
                           contactCardCategory /* contactCard*/);

            // This contactCard category will be published only once.
            contactCardMetaData.ExpiryPolicy = ExpiryPolicy.Persistent;

            // Publish the presence information.
            _endpoint.LocalOwnerPresence.BeginPublishPresence(
                new PresenceCategoryWithMetaData[] { contactCardMetaData, stateMetaData },
                PresencePublishingCompleted, null);
        }

        void PresencePublishingCompleted(IAsyncResult result)
        {
            _endpoint.LocalOwnerPresence.EndPublishPresence(result);
        }

    }
}
